﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MyLibrary.Logger
{
    public class LogWriterBase
    {
        /// <summary>
        /// 文件建立的周期参数
        /// </summary>
        protected enum FileCreateCycle
        {
            Never = 0,
            Hour = 1,
            Day = 2,
            Month = 3,
            Year = 4
        }

        #region 空构造函数
        static LogWriterBase()
        {

        }
        #endregion

        /// <summary>
        /// 初始化化日志写入器
        /// </summary>
        /// <param name="configs">配置集合</param>
        protected virtual void InitLogWriter(object sender, Dictionary<string, string> configs)
        {
            if (sender == null) return;
            //如果配置节中包含了路径配置节
            if (configs.ContainsKey("Path"))
            {

            }
        }
        protected virtual void InitLogWriter(Dictionary<string, string> configs)
        {

        }

        private string PathParser(string path, ref FileCreateCycle cycle)
        {
            //建立替换参数表
            //Dictionary<string, string> pathRelaceArgs = new Dictionary<string, string>();
            //pathRelaceArgs.Add("${APP}", Environment.CurrentDirectory);
            //pathRelaceArgs.Add("${SYS}", Environment.SystemDirectory);
            //pathRelaceArgs.Add("${TMP}", Path.GetTempPath());

            //pathRelaceArgs.Add("@{THOUR}", Path.GetTempPath());
            //pathRelaceArgs.Add("@{TDAY}", Path.GetTempPath());
            //pathRelaceArgs.Add("@{TMONTH}", Path.GetTempPath());
            //pathRelaceArgs.Add("@{TYEAR}", Path.GetTempPath());


            //替换路径参数
            if (path.Contains("${APP}"))
                path = path.Replace("${APP}", Environment.CurrentDirectory);
            if (path.Contains("${SYS}"))
                path = path.Replace("${SYS}", Environment.SystemDirectory);
            if (path.Contains("${TMP}"))
                path = path.Replace("${TMP}", Path.GetTempPath());
            
            //查找文件建立周期参数


        }

        #region 受保护的有关属性
        protected const string DEFAULT_NULL_TEXT = "(null)";
        protected const string DEFAULT_NOT_AVAILABLE_TEXT = "NOT AVAILABLE";
        private string _hostName = String.Empty;
        private string _applicationFriendlyName = String.Empty;
        private DateTime _processStartTime = DateTime.Now;

        protected string NewLine
        {
            get { return Environment.NewLine; }
        }
        protected string ApplicationBaseDirectory
        {
            get { return AppDomain.CurrentDomain.BaseDirectory; }
        }
        protected string ConfigurationFileLocation
        {
            get { return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile; }
        }
        protected string EntryAssemblyLocation
        {
            get { return System.Reflection.Assembly.GetEntryAssembly().Location; }
        }
        protected int CurrentThreadId
        {
            get { return System.Threading.Thread.CurrentThread.ManagedThreadId; }
        } 
        protected string HostName
        {
            get
            {
                if (String.IsNullOrEmpty(_hostName))
                {
                    try
                    {
                        _hostName = System.Net.Dns.GetHostName();
                    }
                    catch (System.Net.Sockets.SocketException)
                    {
                    }
                    catch (System.Security.SecurityException)
                    {
                    }

                    if (String.IsNullOrEmpty(_hostName))
                    {
                        try
                        {
                            _hostName = Environment.MachineName;
                        }
                        catch (InvalidOperationException)
                        {

                        }
                        catch (System.Security.SecurityException)
                        {
                        }
                    }
                    if (String.IsNullOrEmpty(_hostName))
                        _hostName = String.Empty;
                }
                return _hostName;
            }
        }
        protected string ApplicationFriendlyName
        {
            get
            {
                if (String.IsNullOrEmpty(_applicationFriendlyName))
                {
                    try
                    {
                        _applicationFriendlyName = AppDomain.CurrentDomain.FriendlyName;
                    }
                    catch (System.Security.SecurityException)
                    {
                    }

                    if (String.IsNullOrEmpty(_applicationFriendlyName))
                    {
                        try
                        {
                            string assemblyName = EntryAssemblyLocation;
                            _applicationFriendlyName = Path.GetFileName(assemblyName);
                        }
                        catch (System.Security.SecurityException)
                        {
                        }
                    }

                    if (String.IsNullOrEmpty(_applicationFriendlyName))
                        _applicationFriendlyName = String.Empty;
                }
                return _applicationFriendlyName;
            }
        }
        protected DateTime ProcessStartTime
        {
            get { return _processStartTime; }
        }
        #endregion
    }
}
